package cn.itsource.pethome.base.controller;

import cn.itsource.pethome.base.domain.dto.BinderDTO;
import cn.itsource.pethome.base.domain.dto.LoginDTO;
import cn.itsource.pethome.base.domain.dto.WechatDTO;
import cn.itsource.pethome.base.service.ILoginService;
import cn.itsource.pethome.util.AjaxResult;
import cn.itsource.pethome.util.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * LoginController 专门用来做登录
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDTO loginDTO){
        try {
            Map<String, Object> account = loginService.account(loginDTO);
            return AjaxResult.me().setData(account);
        }catch (BusinessException e){//你的业务出现问题了
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 微信登录接口
     * @param wechatDTO
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatDTO wechatDTO){
        try {
            return loginService.wechat(wechatDTO);
        }catch (BusinessException e){//你的业务出现问题了
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PostMapping("/binder")
    public AjaxResult binder(@RequestBody BinderDTO binderDTO){
        try {
            return loginService.binder(binderDTO);
        }catch (BusinessException e){//你的业务出现问题了
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


}
